#!/usr/bin/env python
# -*- coding: utf-8 -*-
#code:utf-8
#
# Copyright(C) 2011 SupDo.com
# Licensed under the GUN License, Version 3.0 (the "License");
# 
# File:        lib
# Author:      KuKei
# Create Date: 2012-09-09
# Description: 公用函数库
# Modify Date: 2012-09-09

import re
from django.template import loader, Context
from django.http import HttpResponse

#===============================================================================
# 函数说明：如果val不存在或者取不到值就返回newval，否则返回val
#===============================================================================
def nvl(val, newVal):
    retVal = None
    try:
        retVal = val if val else newVal
    except Exception, ex:
        retVal = newVal
    finally:
        pass
    return retVal

def toInt(val, newVal=0):
    retVal = newVal
    try:
        retVal = int(val) if val else newVal
    except Exception, ex:
        retVal = newVal
    finally:
        pass
    return retVal

def isHas(dicts, key):
    return True if dicts.has_key(key) and dicts[key] else False

def QSetToDict(querySet):
    retDict = {}
    for e in querySet:
        retDict[e.id] = e
    return retDict

def trimRight(str):
    while(str[-1]=='0'):
        str = str[:-1]
    return str

def getFileName(name):
    tmpList = name.split('.')
    pot = -(len(tmpList[-1])+1)
    return [name[:pot], '.'+ tmpList[-1]]
         
def isEng(str):
    pattern = re.compile(r'\w+')
    return pattern.match(str)

def renderToResponse(path, viewData, theme='default'):
    template = None
    try:
        template = loader.get_template(theme+path)
    except Exception,ex:
        template = loader.get_template('default'+path)
    renderHtml = HttpResponse(template.render(Context(viewData)))
    return renderHtml